package com.socks.library.klog;


import com.socks.library.KLog;
import com.socks.library.KLogUtil;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONException;
import ohos.utils.zson.ZSONObject;


/**
 * Created by zhaokaiqiang on 15/11/18.
 */
public class JsonLog {

    public static void printJson(String tag, String msg, String headString) {

        String message;
        try {
            if (msg.startsWith("{")) {
                ZSONObject jsonObject =  ZSONObject.stringToZSON(msg);
                message = jsonObject.toString();
            } else if (msg.startsWith("[")) {
                ZSONArray jsonArray =  ZSONArray.stringToZSONArray(msg);
                message = jsonArray.toString();
            } else {
                message = msg;
            }
        } catch (ZSONException e) {
            message = msg;
        }

        KLogUtil.printLine(tag, true);
        message = headString + KLog.LINE_SEPARATOR + message;
        String[] lines = message.split(KLog.LINE_SEPARATOR);
        HiLogLabel label  = new HiLogLabel(HiLog.LOG_APP, 0x00201, tag);
        for (String line : lines) {
            HiLog.info(label,"%{public}s","║ " + line);
        }
        KLogUtil.printLine(tag, false);
    }
}
